/*
 * AbstractTable.java
 *
 * Created on November 6, 2006, 9:40 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.samlam;
import java.io.File;
import java.sql.SQLException;
import javax.swing.table.AbstractTableModel;
import java.util.*;
/**
 * Implementation of the AbstractTableModel in the java class library
 * @author Sam
 */
public class MyStuffTableModel extends AbstractTableModel{
    //titles for the columns of the table
    private String[] headings = {"First Name", "Last Name", "Phone Number"};
    // the list that holds the data for the table
    private static List<Object[]> data = new LinkedList<Object[]>();

    
    /** Creates a new instance of TableModel */
    public MyStuffTableModel() {
    }
    
    /**@ Method returns the List of object arrays holding the table values */
    public int getRowCount() {
        return data.size();
    }
    
    /** Method returns the number of columns in the table */
    public int getColumnCount() {
        return headings.length;
    }
    
    /**
     * Gives us the value of the cell in the table according to its row and column
     * @param row This is the row value
     * @param col This is the column value
     * @return Gives the value in the selected cell as a String
     */
    public String getValueAt(int row, int col) {
        Object[] jew = data.get(row);
        return (String)jew[col];
    }
    public Object[] getValueAt(int row){
        return data.get(row);
    }
    
    /** Method returns a boolean value indicating if the cell is editable */
    public boolean isCellEditable(int row, int col) {
            return false;

    }
    
    /**
     * Method returns the selected column title
     * @param col This is the column selected
     * @return This is the name of the column
     */
    public String getColumnName(int col) {
        return headings[col];
    }
    public void setRow(int index,Object[] row){
        data.set(index, row);
        fireTableRowsInserted(index, index);
    }
    /**
     * Method sets the value of the cell
     * @param value This is the new value for the cell
     * @param row The row coordinate
     * @param col the column coordinate
     */
    public void setValueAt(Object value, int row, int col) {
        Object[] old = data.get(row);
        String oldData = (String)old[col];
        //System.out.println("Value " + oldData + " in cell " + row + "," + col + " was changed to " + value);
        
        old[col] = value;
        data.set(row, old);
        fireTableCellUpdated(row, col);
    }
    
    /**
     * Creates a blank row at the bottom of the table
     * @param row The coordinate of the row
     */
    
    public void newRow(int row){
        String[] newRow = {"","","",""};
        data.add(newRow);
        fireTableRowsInserted(row+1, row+1);
    }
    
    /**
     * Creates a new Row for every Object array in the list parameter
     * @param list Contains data that will populate the table
     */
    public void newRow(List<Object[]> list){
        int length = data.size();
        
        //clear the old data
        for(int i = length-1; i >= 0; i--){
            data.remove(i);
        }//end for
        //delete rows from table
        fireTableRowsDeleted(0,length);
        
        //create new rows in GUI
        for (int i = 0; i < list.size(); i++){
            String[] newRow = {"","",""};
            data.add(newRow);
            fireTableRowsInserted(i, i);
        }//end for
        //populate rows with data from the list
        for (int i = 0; i < list.size(); i++){
            Object[] values = list.get(i);
            data.set(i, values);
            fireTableRowsUpdated(i, i);
        }//end for
    }
    

    /** Method returns all of the data being viewed in the table */
    public List getData(){
        return data;
    }
    public String getRowId(int row){
        return (String)data.get(row)[3];        
    }
    
}

